package haierhome.krund.cn.daggertworetrofitrxjavamvp.rxjava.exampletwo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.R;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.bean.UserLogin;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.rxjava.exampletwo.net.ApiService;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * RxJavaRetrofit简单使用
 */

public class RxJavaRetrofitActivity extends AppCompatActivity {

    Button bt_retrofitOne, bt_retrofitTwo, bt_retrofitThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java_retrofit);

        initView();
        initListener();
    }


    Observable.Transformer schedulersTransformer() {
        return new Observable.Transformer() {
            @Override
            public Object call(Object observable) {
                return ((Observable) observable).subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    private void initListener() {
        bt_retrofitOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRequestByRxRetrofitEaseFram();
                //这第一种封装方式  每一个errcode都需要处理。。。。？？？
                //解决方法
                //https://www.jianshu.com/p/5b8b1062866b
            }
        });
        bt_retrofitTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //异常处理
                //https://www.jianshu.com/p/5b8b1062866b
                doRequestByRxRetrofitExceptionFram();
            }
        });
        bt_retrofitThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //https://www.jianshu.com/p/5b8b1062866b
                //变换封装 勤用RXJava compose操作符消除重复代码
            }
        });
    }

    private void initView() {
        bt_retrofitOne = (Button) findViewById(R.id.bt_retrofitOne);
        bt_retrofitTwo = (Button) findViewById(R.id.bt_retrofitTwo);
        bt_retrofitThree = (Button) findViewById(R.id.bt_retrofitThree);
    }

    //retrofit简单封装
    private void doRequestByRxRetrofitEaseFram() {

        ApiService apiService = ApiService.Factory.createService();
        apiService.postLoginRequest("545056@qq.com", "Spl901006ms")
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<UserLogin>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserLogin userLogin) {
                        if (userLogin == null) return;
                        int code = userLogin.getErrorCode();
                        switch (code) {
                            case 1: //do something
                                break;
                            case -101://do something
                                break;
                            case -102: //do something
                                break;
                            default:
                                break;
                        }
                    }
                });
    }

    private void doRequestByRxRetrofitExceptionFram() {

        ApiService mApiService = ApiService.Factory.createService();
        mApiService.postLoginRequest("545056@qq.com", "Spl901006ms")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<UserLogin>(RxJavaRetrofitActivity.this) {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e); //这里就全部交给基类来处理了
                    }

                    @Override
                    public void onNext(UserLogin userLogin) {
                        super.onNext(userLogin);
                        Log.i("@@", "userLogin" + userLogin);
                    }
                });
    }
}
